package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Queue;

public class JDBCUtil {
	private static String url = "jdbc:mysql://titan.cmpe.boun.edu.tr:3306/database8?useUnicode=true&characterEncoding=UTF-8";
	private static Queue<Connection> conQueue = new LinkedList<Connection>();
	private static final int CAPACITY = 3;
	
	public static Connection dequeueConn(){
		if(conQueue.isEmpty()){
			return getConnection();
		}else{
			return conQueue.poll();
		}
	}	
	public static void enqueuConn(Connection con) throws SQLException{
		if(conQueue.size() > CAPACITY){
			con.close();
		}else{
			conQueue.add(con);
		}
	}	
	static {
		try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        } catch (SQLException e) {
            System.out.println("Problem with driver!"+ e.getMessage());
        }
	}
	public static void closeConnection(Connection c){
		try {
			c.close();
		} catch (SQLException e) {
			System.out.println("Problem with ending the connection!");
		}
	}
	public static Connection getConnection() {
		Connection conn=null;
		try {
            conn = DriverManager.getConnection(url, "project8", "Tz1VsO");
        } catch (SQLException e) {
            System.out.println("Problem with connection!" + e.getMessage());
        }
		return conn;
	}
}